package com.hgxd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgxd.annotation.OperationLogAnnotation;
import com.hgxd.entity.pojo.EquipmentRunLog;
import com.hgxd.entity.vo.pagevo.EquipmentRunLogPageVo;
import com.hgxd.result.Result;

import com.hgxd.service.IEquipmentRunLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 设备运行日志：网关、串口服务器 前端控制器
 *
 * @author fsd
 * @since 2023-10-18
 */
@Api(tags = "设备运行日志管理")
@RestController
@RequestMapping("/api/equipment-run-log")
@CrossOrigin
public class EquipmentRunLogController {
    @Resource(name = "EquipmentRunLogServiceImpl")
    private IEquipmentRunLogService equipmentRunLogService;

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "设备运行日志的新增", notes = "新增日志")
    @ApiImplicitParam(name = "equipmentRunLog", value = "运行日志的实体类", required = true)
    @OperationLogAnnotation(operModule = "设备运行日志-新增",operType = "新增",operContent = "新增设备运行日志")
    public Result<String> insert(@RequestBody EquipmentRunLog equipmentRunLog) {
        boolean save = equipmentRunLogService.save(equipmentRunLog);
        return save ? Result.ok("新增成功") : Result.fail("新增失败");
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "设备运行日志的更新", notes = "更新日志")
    @ApiImplicitParam(name = "equipmentRunLog", value = "运行日志的实体类", required = true)
    @OperationLogAnnotation(operModule = "设备运行日志-更新",operType = "更新",operContent = "更新设备运行日志")
    public Result<String> update(@RequestBody EquipmentRunLog equipmentRunLog) {
        boolean data = equipmentRunLogService.updateById(equipmentRunLog);
        return data ? Result.ok("更新成功") : Result.fail("更新失败");
    }

    /**
     * 批量删除
     */
    @PostMapping("/del/batch")
    @ApiOperation(value = "设备运行日志的批量删除", notes = "批量删除日志")
    @ApiImplicitParam(name = "ids", value = "id的list集合", required = true)
    @OperationLogAnnotation(operModule = "设备运行日志-删除",operType = "删除",operContent = "删除设备运行日志")
    public Result<String> deleteBatch(@RequestBody List<Integer> ids) {
        boolean data = equipmentRunLogService.removeByIds(ids);
        return data ? Result.ok("批量删除成功") : Result.fail("批量删除失败");
    }

    /**
     * 查询全部
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询全部设备运行日志", notes = "查询全部设备运行日志")
    @OperationLogAnnotation(operModule = "设备运行日志-查询全部",operType = "查询全部",operContent = "查询全部设备运行日志")
    public Result<List<EquipmentRunLog>> findAll() {
        return Result.ok(equipmentRunLogService.list());
    }

    /**
     * 分页模糊查询
     */
    @PostMapping("/get/page")
    @ApiOperation(value = "分页模糊查询", notes = "分页模糊查询")
    @ApiImplicitParam(name = "equipmentRunLogPageVo", value = "分页工具类")
    @OperationLogAnnotation(operModule = "设备运行日志-分页模糊查询",operType = "分页模糊查询",operContent = "分页模糊查询设备运行日志")
    public Result<Page<EquipmentRunLog>> findPage(@RequestBody EquipmentRunLogPageVo equipmentRunLogPageVo) {
        LambdaQueryWrapper<EquipmentRunLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(EquipmentRunLog::getId);
        lambdaQueryWrapper.eq(!"".equals(equipmentRunLogPageVo.getName()), EquipmentRunLog::getName, equipmentRunLogPageVo.getName());
        lambdaQueryWrapper.eq(!"".equals(equipmentRunLogPageVo.getAlarmType()), EquipmentRunLog::getAlarmType, equipmentRunLogPageVo.getAlarmType());
        lambdaQueryWrapper.eq(EquipmentRunLog::getIsdelete,"0");
        if (!(equipmentRunLogPageVo.getBeginTime() ==null)) {
            lambdaQueryWrapper.between(EquipmentRunLog::getDate, equipmentRunLogPageVo.getBeginTime(), equipmentRunLogPageVo.getEndingTime());
        }
        return Result.ok(equipmentRunLogService.page(new Page<>(equipmentRunLogPageVo.getPageNum(), equipmentRunLogPageVo.getPageSize()), lambdaQueryWrapper));
    }
}

